package com.laozhao.framework.snow.view;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JComponent;
import javax.swing.JMenuBar;
import javax.swing.JTextField;

import com.laozhao.framework.snow.AreaModel;
import com.laozhao.framework.snow.ItemModel;

public class SwingViewUtil {
	
	public final static int ERROR_01 = 11;
	public final static int ERROR_02 = 12;
	public final static int ERROR_03 = 13;
	public final static int ERROR_99 = 99;
	
	public static Map<String, JComponent> objMap = new HashMap<String, JComponent>();

	public static Map<String, String> classMap = new HashMap<String, String>() {
		{
			put("area.menubar", "javax.swing.JMenubar");
			put("area.toolbar", "javax.swing.JToolBar");
			put("area.search", "javax.swing.JPanel");
			put("area.table", "javax.swing.JTable");
			put("area.editor", "javax.swing.JToolBar");
			put("area.navigation", "javax.swing.JScrollPane");
			put("area.messages", "javax.swing.JPanel");
			
			put("item.text", "javax.swing.JTextField");
			put("item.label", "javax.swing.JLabel");
			put("item.radio", "javax.swing.JRadioButton");
			put("item.check", "javax.swing.JCheckBox");
			put("item.menu", "javax.swing.JMenuItem");
			put("item.radiomenu", "javax.swing.JRadioButtonMenuItem");
			put("item.checkmenu", "javax.swing.JCheckBoxMenuItem");
		}
	};

	public static JComponent creaSwingComponent(AreaModel area) {
		JComponent reComponent = null;
		String type = "area." + area.getType();		
		try {
			reComponent =  (JComponent) Class.forName(classMap.get(type)).newInstance();
			// 
			Method[] methods = reComponent.getClass().getMethods();
			
			if (reComponent instanceof JTextField) {
				
			}
		} catch (InstantiationException e) {
			e.printStackTrace();
			System.exit(ERROR_01);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			System.exit(ERROR_02);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			System.exit(ERROR_03);
		}catch (Exception e) {
			e.printStackTrace();
			System.exit(ERROR_99);
		}
		return reComponent;
	}

	public static JComponent creaSwingComponent(ItemModel item) {
		String type = "item." + item.getType();
		try {
			return (JComponent) Class.forName(classMap.get(type)).newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
			System.exit(ERROR_01);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			System.exit(ERROR_02);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			System.exit(ERROR_03);
		}catch (Exception e) {
			e.printStackTrace();
			System.exit(ERROR_99);
		}
		return null;
	}
	
	public JComponent get(String id){
		if (objMap.keySet().contains(id)) {
			return objMap.get(id);
		}
		return null;
	}
	
	public Object getValue(String id){
		
		return null;
	}
	
	public void setValue(String id,Object value){
		
	}

	private static JComponent setMessageBar(AreaModel area) {
		// TODO Auto-generated method stub
		return null;
	}

	private static JComponent setToolBar(AreaModel area) {
		// TODO Auto-generated method stub
		return null;
	}

	private static JComponent setViewPanel(AreaModel area) {
		// TODO Auto-generated method stub
		return null;
	}

	private static JComponent setSearchPanel(AreaModel area) {
		// TODO Auto-generated method stub
		return null;
	}

	private static JComponent setNavigationBar(AreaModel area) {
		// TODO Auto-generated method stub
		return null;
	}

	private static JComponent setEditorPanel(AreaModel area) {
		// TODO Auto-generated method stub
		return null;
	}

	private static JComponent setMenuBar(AreaModel area) {
		JMenuBar menuBar = new JMenuBar();
		if (area.getDescription() != null && !"".equals("")) {
			menuBar.setToolTipText(area.getDescription() );
		}
//		if (area.getIcon() != null) {
//			
//		}
//		if (area.getParam() != null) {
//			
//		}
		
		
		return null;
	}
	
}
